package mashuaiproject.mashuaiproject.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import mashuaiproject.mashuaiproject.dto.CommentListDto;
import mashuaiproject.mashuaiproject.dto.NotificationDto;
import mashuaiproject.mashuaiproject.dto.PaginationDto;
import mashuaiproject.mashuaiproject.mapper.NotificationMapper;
import mashuaiproject.mashuaiproject.model.Notification;
import mashuaiproject.mashuaiproject.model.Users;
import mashuaiproject.mashuaiproject.serivce.NotificationSerivce;
import mashuaiproject.mashuaiproject.serivce.QuesionUserSerivce;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProfileController {
    @Autowired
    private QuesionUserSerivce quesionUserSerivce;
    @Autowired
    private NotificationSerivce notificationSerivce;
    @Autowired
    private NotificationMapper notificationMapper;
    //这个写法可与i让一个页面起到公用作用s
    //这个请求是记录有没有读信息
    @GetMapping("/profile/{action}")
    public String  profile(@PathVariable(name = "action") String action
            , Model model
            , HttpServletRequest request
            , @RequestParam(name = "page",defaultValue = "1")Integer page
            ,@RequestParam(name ="size",defaultValue = "6") Integer size
            ,@RequestParam(name ="sort",defaultValue = "no" ,required = false) String  sort){
        System.err.println("结束的排序："+sort);
        Users users= (Users) request.getSession().getAttribute("user");

        if(users!=null) {
            if (action.contains("quesions")) {
                if(!action.equals("quesions")){
                    String quesionIds=action.split("-")[1];
                    Integer quesionId=Integer.valueOf(quesionIds);
                    int delQueiosns=quesionUserSerivce.delQueiosnId(users.getId(),quesionId);//为了安全起见，需要判断用户是不是对应的
                }
                model.addAttribute("section", "quesions");
                model.addAttribute("sectionName", "我的提问");
                PaginationDto paginationDto = quesionUserSerivce.selectQ_UList(users.getId(), page, size);

                model.addAttribute("paginationDto", paginationDto);
                model.addAttribute("isOtherLook",null);
            }
            //这是封装的用户查看博主的信息
            if(action.contains("autor")){
               //根据文章的id查找到当前用户的id
                String queisonsId=action.split("-")[1];
                Integer queiosnIs=Integer.valueOf(queisonsId);
                Integer crator=quesionUserSerivce.selectCreator(queiosnIs);//用户的id
                Users user=quesionUserSerivce.slectUser(crator);

                PaginationDto paginationDto = quesionUserSerivce.selectQ_UList(crator, page, size);
                model.addAttribute("paginationDto", paginationDto);
                model.addAttribute("section", "autor");
                model.addAttribute("sectionName", user.getName()+"的博客");
                model.addAttribute("isOtherLook","yes");
                model.addAttribute("quesionId",queiosnIs);//当前文章的id
                model.addAttribute("homeUser",user);//当前文章的id

            }

            if (action.equals("replies")) {
                System.out.println("当前页数："+page);
                Integer cruuentPage=1;//默认第一页
                Integer totlolCount=notificationMapper.sleectCount(users.getId());
                //判断
                Integer countPage=0;//总数
                Integer sizes=6;
                if(totlolCount%sizes==0){
                    countPage=totlolCount/sizes;
                }else{
                    countPage=totlolCount/sizes+1;
                }

                //最新回复通知内容
                model.addAttribute("section", "replies");
                model.addAttribute("sectionName", "回复我的");
                //查找出当前用户收到的所有回复
                List<Notification> notificationsList = notificationSerivce.selectCurrentNotified(users.getId(),page);
                List<NotificationDto> notificationDtos = new ArrayList<>();
                //找出这些信息的回复人
                if (notificationsList.size() != 0) {
                    for (Notification nos : notificationsList) {
                        NotificationDto notificationDto = new NotificationDto();
                        Users users1 = notificationSerivce.selectNotifiedUsers(nos.getNotifier());//查找到每个回复信息的回复人
                        //找到每个用户回复的内容
                        if (nos.getNotifier() == users1.getId()) {
                            BeanUtils.copyProperties(nos, notificationDto);
                            notificationDto.setNotified_user(users1);
                            //获取到回复标题
                            if(nos.getType()==1){
                                //这是回复文章的
                                String  reciverQuesion=notificationSerivce.slectReciverQuesion(nos.getQuesion_id());
                                notificationDto.setReciverQuesion(reciverQuesion);
                            }else if(nos.getType()==2){
                                //这是回复评论的
                                String reciverComment=notificationSerivce.selectReciverComment(nos.getComment_id());
                                notificationDto.setReciverComment(reciverComment);
                            }
                            notificationDtos.add(notificationDto);
                            //打印下封装的对象
                        }
                    }
                }
                //找出为读书
                 Integer noReadConut=notificationSerivce.selectNoReadCount(users.getId());
                request.getSession().setAttribute("noReadConut",noReadConut);//将未读数量放假session
                model.addAttribute("noReadConut",noReadConut);
                model.addAttribute("notificationDtos",notificationDtos);
                model.addAttribute("cruuentPages",page);
                model.addAttribute("countPage",countPage);
                model.addAttribute("isOtherLook",null);
            }

            //查看评论
            if(action.equals("comment")){
                List<CommentListDto> commentListDtos=quesionUserSerivce.selectOneComment(users.getId(),page,8,sort);
                //判断是否已经关注过
                model.addAttribute("commentListDtos",commentListDtos);
                model.addAttribute("isOtherLook",null);
                model.addAttribute("section", "comment");
                model.addAttribute("sectionName","我参与的评论");

                String sortTime="timeUp";
                String sortFabulos="fabulousUp";
                String sortHot="hotUp";
                if(!sort.equals("no")){
                    if(sort.equals("timeUp")){
                        sortTime="timeDown";
                        model.addAttribute("sort","timeUp");
                    }
                    if(sort.equals("timeDown")){
                        sortTime="timeUp";
                        model.addAttribute("sort","timeDown");
                    }
                    if(sort.equals("fabulousUp")){
                        sortFabulos="fabulousDown";
                        model.addAttribute("sort","fabulousUp");

                    }
                    if(sort.equals("fabulousDown")){
                        sortFabulos="fabulousUp";
                        model.addAttribute("sort","fabulousDown");
                    }

                }else {
                    model.addAttribute("sort","no");

                }
                model.addAttribute("sortTime",sortTime);
                model.addAttribute("sortFabulos",sortFabulos);
                model.addAttribute("sortHot",sortHot);//默认都是降序

                //给于三个默认的排序
            }

        }


        return "profile";
    }

}
